#include "stm32f10x.h"                  // Device header
extern uint16_t ADF[];
void AD_Init(void)
{
	RCC_APB2PeriphClockCmd (RCC_APB2Periph_ADC1 ,ENABLE );
	RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA ,ENABLE );
	RCC_ADCCLKConfig(RCC_PCLK2_Div6);//ADC预分频6分频，12MHz
	RCC_AHBPeriphClockCmd (RCC_AHBPeriph_DMA1,ENABLE);//开启RCC时钟

	GPIO_InitTypeDef GPIO_InitStructure;
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模拟输入模式
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);
	//规则组参数设置，通道0，序列1，采样时间55.5个ADC周期
	ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_55Cycles5);
	//规则组参数设置，通道1，序列2，采样时间55.5个ADC周期

	ADC_InitTypeDef ADC_InitStruct;
	ADC_InitStruct.ADC_ContinuousConvMode = ENABLE ;//连续转换
	ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;//采样数据右对齐
	ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//软件触发
	ADC_InitStruct.ADC_Mode = ADC_Mode_Independent;//独立模式
	ADC_InitStruct.ADC_NbrOfChannel = 2;//2个通道。仅扫描模式有效
	ADC_InitStruct.ADC_ScanConvMode = ENABLE  ;//扫描模式
	ADC_Init(ADC1, &ADC_InitStruct);
	
	DMA_InitTypeDef DMA_InitStruct;
	DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t )&ADC1->DR ;//传入源ADC寄存器地址
	DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//源数据16位
	DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//源地址不自增
	DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)ADF;//传入目标数组地址
	DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//目标数据16位
	DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;//目标地址自增
	DMA_InitStruct.DMA_BufferSize = 4;//传入指定传输计数器数值
	DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;//数据方向源数组a到目标数组b
	DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;//使用硬件触发
	DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;//传输计数器重装
	DMA_InitStruct.DMA_Priority = DMA_Priority_High;//设置通道优先级为高
	DMA_Init( DMA1_Channel1, &DMA_InitStruct);
	DMA_Cmd(DMA1_Channel1, ENABLE);//开启DMA转运

	ADC_DMACmd(ADC1, ENABLE);//开启DMA通道
	ADC_Cmd(ADC1,ENABLE );//开启ADC
	
	ADC_ResetCalibration(ADC1);//复位校准
	while ( ADC_GetResetCalibrationStatus(ADC1) == SET );//等待校准完成
	ADC_StartCalibration(ADC1);//开始校准
	while ( ADC_GetCalibrationStatus(ADC1) == SET );//等待校准完成
	
	ADC_SoftwareStartConvCmd(ADC1, ENABLE);//软件触发转换
}
